package org.loofer.ext.utils;

import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.Canvas;
import android.media.ExifInterface;
import android.text.TextUtils;
import java.io.ByteArrayOutputStream;
import java.io.File;

public final class BitmapUtils {
    private static final int DEFAULT_QUALITY = 90;

    private BitmapUtils() {
    }

    public static byte[] compressToBytes(Bitmap bitmap) {
        return compressToBytes(bitmap, 90, bitmap.hasAlpha() ? CompressFormat.PNG : CompressFormat.JPEG);
    }

    public static byte[] compressToBytes(Bitmap bitmap, CompressFormat format) {
        return compressToBytes(bitmap, 90, format);
    }

    public static byte[] compressToBytes(Bitmap bitmap, int quality, CompressFormat format) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream(65536);
        bitmap.compress(format, quality, baos);
        return baos.toByteArray();
    }

    @SuppressLint({"NewApi"})
    public static Bitmap rotateBitmapByExif(Bitmap bitmap, String path) {
        if (bitmap == null || TextUtils.isEmpty(path)) {
            return bitmap;
        }
        ExifInterface exif = null;
        try {
            exif = new File(path).exists() ? new ExifInterface(path) : null;
        } catch (Throwable th) {
        }
        return exif != null ? rotateBitmapByExif(bitmap, exif) : bitmap;
    }

    @SuppressLint({"NewApi"})
    public static Bitmap rotateBitmapByExif(Bitmap bitmap, ExifInterface exif) {
        if (bitmap == null || exif == null) {
            return bitmap;
        }
        int rotation = 0;
        switch (exif.getAttributeInt("Orientation", 0)) {
            case 3:
                rotation = 180;
                break;
            case 6:
                rotation = 90;
                break;
            case 8:
                rotation = 270;
                break;
        }
        return rotateBitmap(bitmap, rotation);
    }

    public static Bitmap rotateBitmap(Bitmap bitmap, int rotation) {
        rotation %= 360;
        if (rotation == 0) {
            return bitmap;
        }
        boolean rotateDimension = (rotation > 45 && rotation < 135) || (rotation > 225 && rotation < 315);
        int width = !rotateDimension ? bitmap.getWidth() : bitmap.getHeight();
        int height = !rotateDimension ? bitmap.getHeight() : bitmap.getWidth();
        Bitmap newBitmap = null;
        try {
            newBitmap = Bitmap.createBitmap(width, height, bitmap.getConfig());
        } catch (Throwable th) {
        }
        if (newBitmap == null || newBitmap == bitmap) {
            return bitmap;
        }
        Canvas canvas = new Canvas(newBitmap);
        int dx = (width - bitmap.getWidth()) / 2;
        int dy = (height - bitmap.getHeight()) / 2;
        if (!(dx == 0 && dy == 0)) {
            canvas.translate((float) dx, (float) dy);
        }
        canvas.rotate((float) rotation, (float) (bitmap.getWidth() / 2), (float) (bitmap.getHeight() / 2));
        canvas.drawBitmap(bitmap, 0.0f, 0.0f, null);
        bitmap.recycle();
        return newBitmap;
    }
}
